<!-- pages/points/points.vue -->
<template>
	<view class="container">
		<!-- 标题 -->
		<!-- <CustomNavbar title="积分交易" dispaly="none" :showBack="true" /> -->
		<my-points></my-points>
		<scroll-view scroll-y @scrolltolower="loadMore" :lower-threshold="100" class="points-list">
			<view class="card" v-for="card in cardList" :key="card.id">
				<view class="card-info">
					<view class="icon">
					<image src="/goodsPages/static/transaction.png" mode="widthFix"></image>
					<text>{{card.score}}</text>
					</view>
					<view class="info">
						<text class="title">{{card.title}}</text>
						<view class="price-wrapper">
							<text>￥</text>
							<text class="price">{{card.price}}</text>
						</view>
					</view>
				</view>
				<button @click="buyScore(card)">{{card.text}}</button>
			</view>
			<!-- 加载状态提示 -->
			<u-loadmore :status="loadStatus" :loading-text="loadingText" :loadmore-text="loadmoreText"
				:nomore-text="nomoreText" marginTop="72rpx" color="#999" lineColor="#D9DCE2" line lineWidth="99rpx" />
		</scroll-view>
		<pay-password v-model:visible="showPayPassword" :amount="payInfo?.publish_type == '1' ? 0 : payInfo?.price" @complete="handlePayComplete"
			@forget="handleForgetPassword" />
	</view>

</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import CustomNavbar from '@/components/CustomNavbar.vue'
	import MyPoints from '@/components/MyPoints.vue'

	import PayPassword from '@/components/PayPassword.vue'
	const showPayPassword = ref(false)

	// 列表数据
	const cardList = ref([])
	// 当前页码
	const currentPage = ref(1)
	// 是否正在加载
	const loading = ref(false)
	// 加载状态提示
	const loadingText = ref('努力加载中');
	const loadmoreText = ref('滑动加载更多');
	const nomoreText = ref('实在没有了');
	// 是否还有更多数据
	const hasMore = ref(true)
	// 滚动区域高度
	const scrollHeight = ref(0)
	// 加载状态
	const loadStatus = ref('loadmore')


	// 获取列表数据
	const getListData = async (page = 1) => {
		try {
			loading.value = true
			loadStatus.value = 'loading'

			// 模拟接口请求
			uni.$api.score.scoreList({
				page: currentPage.value
			}).then(res => {
				if (res.code == 200) {
					if (res.data.data.length > 0) {
						if (page === 1) cardList.value = []
						cardList.value = [...cardList.value, ...res.data.data]

						hasMore.value = res.data.current_page < res.data.last_page
						loadStatus.value = hasMore.value ? 'loadmore' : 'nomore'
					} else {
						noMore.value = true
					}
				}
			})


		} finally {
			loading.value = false
		}
	}

	// 加载更多
	const loadMore = () => {
		if (!loading.value && hasMore.value) {
			currentPage.value++
			getListData(currentPage.value)
		}
	}
	const payInfo = ref(null)
	//买入
	const buyScore = (item) => {
		payInfo.value = item;
		showPayPassword.value = true
	}
	// 支付完成回调
	const handlePayComplete = (password) => {
		uni.showLoading({
			title: '支付处理中...'
		})

		uni.hideLoading()
		uni.$api.score.scoreExchange({
			id: payInfo.value.id,
			pay_password: password,
			text: payInfo.value.text
		}).then(res => {
			uni.showToast({
				title: res.msg
			})
			getListData(1)
			setTimeout(function() {
				showPayPassword.value = false
			}, 1000)
		}).catch(err => {
			setTimeout(function() {
				showPayPassword.value = false
			}, 1000)
		})
	}

	// 忘记密码
	const handleForgetPassword = () => {
		uni.showModal({
			title: '提示',
			content: '请通过安全中心重置支付密码',
			showCancel: false
		})
	}

	// 初始化加载
	onMounted(() => {
		// 计算滚动高度（根据你的页面布局调整）
		scrollHeight.value = uni.getSystemInfoSync().windowHeight - 50
		getListData(1)
	})
</script>

<style lang="scss" scoped>
	* {
		margin: 0;
		padding: 0;
	}

	page {
		overflow-y: hidden;
	}

	.container {
		// padding-bottom: 120rpx;
		background: url('/static/index_slices/index_bg.png') no-repeat;
		background-size: 100% auto;
		padding-top: 28rpx;

		/* 导航栏高度 */
		.points-list {
			border-radius: 46rpx 46rpx 0 0;
			margin-top: -36rpx;
			background-color: #EFF2F6;
			padding: 30rpx;
			box-sizing: border-box;
			height: calc(100vh - 160px);

			.card {
				background-color: #fff;
				border-radius: 26rpx;
				margin-top: 20rpx;
				padding: 20rpx 21rpx 24rpx 13rpx;
				display: flex;
				justify-content: space-between;
				align-items: baseline;
				position: relative;
				.icon{
					position: relative;
					text{
						display: block;
						position: absolute;
						left: 50%;
						top: 33%;
						transform: translate(-50%,-50%);
						font-weight: bold;
						color: #b67536;
					}
				}
				.card-info {
					display: flex;
					align-items: center;

					image {
						width: 120rpx;
						height: 120rpx;
					}

					.info {
						padding: 14rpx 0;
						font-size: 26rpx;
						color: #000;
						margin-left: 18rpx;
						font-weight: bolder;

						.price-wrapper {
							margin-top: 38rpx;
							color: #DD3A22;
							font-size: 24rpx;

							.price {
								font-size: 30rpx
							}
						}
					}
				}

				button {
					color: #fff;
					background: linear-gradient(to right, #FE474F, #FA6261);
					width: 100rpx;
					height: 50rpx;
					line-height: 50rpx;
					font-size: 24rpx;
					border-radius: 25rpx;
					opacity: 0.8;
					position: relative;
					top: -4rpx;
				}
			}
		}
	}
</style>